<?php
/**
 * 
 * 实现功能：
 * 实现了入口的导航逻辑
 * 项目的入口 （也是接收code的入口）
 * http://192.168.33.10/CI/index.php/main_control/entrance
 * 
 * 
 * 具体说明：
 * 回调页面硬编码了入口URL（ base_url().'/index.php/main_control/entrance'）
 * 		a)微信回调页面获取code：http://12002shop.web08.com.cn/hfjx/wx/codeback.html
 * 		b)通过回调页面上的提交按钮将code提交到入口URL（ base_url().'/index.php/main_control/entrance'）
 * 		c)获取到openId，根据openId得到user 然后根据结果跳转到注册、审核提醒、首页的view
 * 
 * 关键点说明：
 * 1) debug模式的修改
 *     _myconfig.php : $config['mydebug']
 *     debug模式主要是为了在本地设置openId
 * 2） entrance() 方法	
 * 		该方法对应view（view/jjr/entrance.php） 页面中使用了sessionStorage缓存，主要保存了openId等相关信息
 *      方便在导航跳转时候识别客户
 * 
 *  相关配置参数：
 *  _myconfig.php : $config['json_url_base']   $config['mydebug']
 *  database.php
 *  config.php : $config['base_url']
 * 
 * 
 */

class Main_Control extends CI_Controller {

	function entrance(){
		$this->load->helper('url');
		
		$this->config->load('_myconfig.php');
		$title = $this->config->item('mytitle');
		
		$debug = $this->config->item('mydebug');
		
		if($debug==false){	
			$code = isset($_POST['code'])?$_POST['code']:'';
			
			if($code==''){
				echo "网络故障请重新登陆";
				die();
			}
			
			$this->load->model('wechat_model');
			$openId = $this->wechat_model->getOpenId($code);	// 由code获取openId
			
			if($openId==''){
				echo "网络故障请重新登陆";
				die();
			}
		
		}else 
			$openId="aaa";
		
		$this->load->model('user_model');
		$user = $this->user_model->getUserByOpenId($openId);	//由openId查询user
		$state = isset($user['state'])?$user['state']:-1;
		
		
		$name = isset($user['name'])?$user['name']:'经纪人';
		$uid = isset($user['id'])?$user['id']:0;
		
		$data['openId']=$openId;	// 便于entrance.php缓存使用，方便在导航跳转时候识别客户
		$data['uid']=$uid;
		$data['state']=$state;
		$data['name']=$name;
		$data['title']=$title;
		
		$this->load->view('jjr/index',$data);
		
	}
	
	/*
	 * 单击入口按钮提交后的导航逻辑
	 * state:-1,0,1 三中状态决定了导航到“注册”、“首页”还是“待审核”页面
	 * 
	 */
	function entrance_do(){
		$this->load->helper('url');
		
		$this->config->load('_myconfig.php');
		$title = $this->config->item('mytitle');
		
// 		下面3句可以用缓存来取代
		$name = isset($_POST['name'])?$_POST['name']:'';
		$openId = isset($_POST['openId'])?$_POST['openId']:'';
		$uid = isset($_POST['uid'])?$_POST['uid']:0;
		$state = isset($_POST['state'])?$_POST['state']:-1;
		$flag = isset($_POST['flag'])?$_POST['flag']:0;
		
		$data['flag']=$flag;
		$data['openId']=$openId;
		$data['uid']=$uid;
		
// 		echo $state;
		
		if($state==-1){
			$data['name']=$name;
			$data['title']=$title.' | 注册';
			$data['state']=$state;
			$this->load->view('jjr/regist1',$data);
		}elseif ($state==0){
			$data['name']=$name;
			$data['title']=$title.' | 信息提示';
			$data['msg']="审核中，请等待...";
			$this->load->view('jjr/_msg',$data);
		}elseif ($state==1){
			$data['name']=$name;
			$data['state']=$state;
// 			$data['title']=$title.' | 首页';
// 			$this->load->view('jjr/main',$data);
			
			$data['title']='推荐客户';
			$this->load->view('jjr/client1',$data);
		}elseif ($state==2){
			$data['name']=$name;
			$data['title']=$title.' | 信息提示';
			$data['msg']="对不起，您的审核请求未被通过或账户被停用";
			$this->load->view('jjr/_msg',$data);
		}
		
// 		print_r($data);
		
	}
	
// 	http://192.168.33.10/CI/index.php/main_control
	function test_main(){
		$this->load->helper('url');
		$data['title']=' 经纪人 | 首页';
		$this->load->view('jjr/index',$data);
	}
	
	function test_finish(){
		$this->load->helper('url');
		$data['title']=$title.' | 推荐客户成功';
		$this->load->view('jjr/client3',$data);
	}
	
	
}